﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Gladkikh.Sims2.Combiner.Database;

namespace Gladkikh.Sims2.Combiner.Controls
{
    class Sims2RegBuilder
    {
        static private string CreateSuppress(List<Sims2Addon> addons, int k)
        {
            string res = "";

            if (k == 0)
                return "Sims2.exe";

            for (int i = 0; i < k; i++)
            {
                if (addons[i].IsOk)
                    if (i != 0)
                        res = addons[i].Exe + "," + res;
                    else
                        res = addons[i].Exe;
                else
                    if (i != 0)
                        res = "," + res;
                    else
                        res = ",";
            }
            return res;
        }

        static string EPInstalled(List<Sims2Addon> addons)
        {
            string res = "";

            int max = 0;
            for (int i = 0; i < addons.Count; i++)
                if (addons[i].IsOk)
                    max = i;

            for (int i = 1; i <= max; i++)
            {
                if (addons[i].IsOk)
                    if (i != 1)
                        res = res + "," + addons[i].Exe;
                    else
                        res = addons[i].Exe;
                else
                    if (i != 1)
                        res = res + ",";
                    else
                        res = ",";
            }
            return res;

        }

        static public void MakeBackup()
        {
            ///Создание бэкапа
            ///
            Directory.CreateDirectory("reg_backup");
            string name = String.Format("\\reg_backup\\Backup.{0:0000}{1:00}{2:00}{3:00}{4:00}.Sims2Reg.reg", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            string file = String.Format("/E \"{0}\" \"HKEY_LOCAL_MACHINE\\SOFTWARE\\EA Games\\\"",
                    Path.GetDirectoryName(Application.ExecutablePath) + name);
            System.Diagnostics.Process.Start("regedit.exe", file);

            name = String.Format("\\reg_backup\\Backup.{0:0000}{1:00}{2:00}{3:00}{4:00}.Sim2CDreg.reg", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            file = String.Format("/E \"{0}\" \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\EA Games\\\"",
                    Path.GetDirectoryName(Application.ExecutablePath) + name);
            System.Diagnostics.Process.Start("regedit.exe", file);
        }

        static public void Build(List<Sims2Addon> addons)
        {
            string reg = "Windows Registry Editor Version 5.00\r\n";

            if (addons[0].IsOk)
                reg += addons[0].GetReg(EPInstalled(addons));

            for (int i = 1; i < addons.Count; i++)
            {
                if (addons[i].IsOk)
                    reg += addons[i].GetReg(CreateSuppress(addons,i));

            }

            StreamWriter sr = new StreamWriter("reg.reg", false, Encoding.Default);
            sr.Write(reg);
            sr.Close();

            MakeBackup();
           
            System.Diagnostics.Process.Start(@"regedit.exe", "reg.reg");
        }
    }
}
